﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CashOrCard.Helper;
using CashOrCard.Models;
using CashOrCard.Repository;
using CashOrCard.Extensions;

namespace CashOrCard.Controllers
{
    public class PaymentHistoryController : Controller
    {
        private readonly IBaseRepository<PaymentHistory> _paymentRep;
        private readonly IBaseRepository<User> _userRep;
        
        public PaymentHistoryController(IBaseRepository<PaymentHistory> paymentRep, IBaseRepository<User> userRep)
        {
            this._paymentRep = paymentRep;
            this._userRep = userRep;
        }
        
        private User getCurrentUser()
        {
            string email = Common.CurrentUserEamil;
            User curUser = _userRep.LoadEntities(x => x.Email == email && x.IsEnable == true).FirstOrDefault();
            return curUser;
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "paymenthistory_access", ErrorView = "PermissionDenied")]
        public ActionResult Index()
        {
            return View();
        }

        [CashOrCardAuthorizeExtension(AllowPermissions = "paymenthistory_access", ErrorView = "PermissionDenied")]
        public ActionResult GetAllHistories(string paymentType)
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            if(string.IsNullOrEmpty(paymentType))
                paymentType = "";
            IQueryable<PaymentHistory> phs = null;
            int total = 0;
            if (Common.IsAdmin)
            {
                phs = _paymentRep.LoadPagerEntities(pageSize, pageIndex, out total, 
                                                        u => 
                                                       (string.IsNullOrEmpty(paymentType) ||
                                                        u.PaymentType.ToLower().Contains(paymentType.ToLower()))
                                                       && u.PaymentResult, true, u => u.PaymentId);          
            }
            else
            {
                var curUser = getCurrentUser();
                total = curUser.PaymentHistories.Count();
                phs = curUser.PaymentHistories.Where(u => u.PaymentResult && (string.IsNullOrEmpty(paymentType) ||
                                                        u.PaymentType.ToLower().Contains(paymentType.ToLower()))).Skip(pageSize * (pageIndex - 1)).Take(pageSize).OrderBy(o => o.PaymentId).AsQueryable();
            }
            var list = (from ph in phs
                        select new
                        {
                            PaymentId = ph.PaymentId,
                            PaymentAmount = ph.PaymentAmount,
                            PaymentComment = ph.PaymentComment,
                            PaymentDate = ph.PaymentDate.ToString("MM/dd/yyyy"),
                            PaymentResult = ph.PaymentResult
                        }).ToList();
            var result = new { total = total, rows = list };
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetPaymentType()
        {
            var types = Common.GetEnums(typeof(PaymentType));
            return Json(types, JsonRequestBehavior.AllowGet);

        }
    }
}
